import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'dva'
import UserList from './List'
import { message, Modal } from 'antd';
const confirm = Modal.confirm;

function User({location, dispatch, price, loading}) {
const {record, selectedRowKeys}=price;
  function onDeleteItem(id) {
    dispatch({
      type: 'price/delete2',
       payload: id
     })
  }
/**
 * 修改
 */
  function onEditItem(values) {
    dispatch({
      type: 'price/modify',
      payload: values

    })
  }


  function onStatusItem(item) {
    dispatch({
      type: 'user/updateStatus',
      payload: {
        curItem: item
      }
    })
  }

/**
 * 批量删除
 */
  function onDeleteBatch(ids) {
    dispatch({
      type: 'price/deleteBatch',
      payload: { ids }
    })
  }

/**
 * 查看
 */
  function onSearch(values){
    dispatch({
      type: 'price/search',
      payload: values
    })
  }

  /**
   * Button 删除
   */
  function onDeleteItem1() {
    if(!record){ //未定义状态判断
      message.warning('请至少选中一条数据！');
      return;
    }else if(record.length>0){
      const id=selectedRowKeys;
      confirm({
        title:'确定删除么？',
        onOk(){
          dispatch({
            type:'price/delete',
            payload:{id}
          })
        },
      })
    }else {
      message.warning('请至少选中一条数据！')
      return;
    }
  }

  /**
   * 增加
   */
  function onCreate(values){
    dispatch({
      type: 'price/create',
      payload: values
    })
  }

  const listProps = {
    price,
    loading,
    location,
    onDeleteItem,
    onEditItem,
    onStatusItem,
    onDeleteItem1,
    onDeleteBatch,
    onSearch,
    onCreate,
    dispatch
  }

  return (
    <div className='content-inner'>

      <UserList {...listProps}/>

    </div>
  );
  }

  User.propTypes = {
  price: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func
  };

  function mapStateToProps({ price }){
  return { price }
  }

  export default connect(mapStateToProps)(User);
